<?php
/**
 * 飞信机器人--数据库简单包装
 * 功能： 数据库类
 * 作者： Kelvin
 * 联系： msn:shichangguo@msn.com, qq:191848423
*/

class CDb
{
	var $db_link;

	var $db_host;
	var $db_name;
	var $db_user;
	var $db_pwd;

	function CDb($svcMobileNo)
	{
		global $DB_ARRAY;
		$this->db_host = DB_HOST;
		//$this->db_name = "fx$svcMobileNo";
		$this->db_name = DB_NAME;
		$this->db_user = DB_USER;
		$this->db_pwd  = DB_PWD;
	}

	function connect()
	{
		if( !isset( $this->db_link ) )
		{
			$this->db_link = mysql_connect($this->db_host, $this->db_user, $this->db_pwd );
			DEBUG_MSG('Db::connect:'. $this->db_link?"Success,{$this->db_name}":"Failed,{$this->db_name}");
			if( !$this->db_link )die();
			
			mysql_select_db( $this->db_name ) or DEBUG_MSG('Db::connect: select db failed{$this->db_name}:'. mysql_error(),true);
			
			mysql_query('set names utf8');
		}
	}

	function fetchQueryAll($sql)
	{
		$this->connect();
		DEBUG_MSG('Db::fetchQueryAll:'.$sql);
		
		$result = mysql_query( $sql );
		if( !$result )
		{
			return array();
		}
		
		$retArr = array();
		while( $arr = mysql_fetch_array( $result, MYSQL_ASSOC ) )
		{
			$retArr[] = $arr;
		}

		if($result)mysql_free_result( $result );
		return $retArr;
	}

	function execute($sql)
	{
		$this->connect();
		DEBUG_MSG('Db::execute:'.$sql);

		return mysql_query( $sql );
	}

}

?>
